package com.shows.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shows.auto.JSONAuthentication;
import com.shows.entity.ShDress;
import com.shows.entity.ShOrder;
import com.shows.entity.ShUser;
import com.shows.mapper.ShDressMapper;
import com.shows.mapper.ShOrderMapper;
import com.shows.mapper.ShUserMapper;
import com.shows.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author animal
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/sh-order")
public class ShOrderController extends JSONAuthentication {
    @Autowired
    ShOrderMapper shOrderMapper;
    @Autowired
    ShDressMapper shDressMapper;
    @Autowired
    ShUserMapper shUserMapper;

    @ApiOperation(value = "添加订单")
    @PostMapping("/add")
    public void add(HttpServletRequest request, HttpServletResponse response, @RequestBody ShOrder shOrder) throws Exception{
        Date date=new Date();
        Timestamp timestamp=new Timestamp(date.getTime());
        shOrder.setCreatTime(timestamp);
        float zhifu= shOrder.getPrice();
      ShUser shUser=  shUserMapper.selectById(shOrder.getUserId());
    float blance= shUser.getBalance();
    if(blance-zhifu>0){
        int i=shOrderMapper.insert(shOrder);
        shUser.setBalance(blance-zhifu);
        if(i>0&&shUserMapper.updateById(shUser)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }else{
        Result error = Result.error().message("余额不足");
        this.WriteJSON(request, response, error);
    }

    }
    @ApiOperation(value = "删除订单")
    @PostMapping("/del")
    public void delOrder(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=shOrderMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有订单")
    @PostMapping("/findAllOrder")
    public void findById(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<ShOrder> shUserPage=new Page<>(current,size);
        Page<ShOrder> shUserPages=shOrderMapper.selectPage(shUserPage,null);
        List<ShOrder> list = shUserPages.getRecords();
        int total=shOrderMapper.selectCount(null);
        for(ShOrder a:list){
        a.setShUser(shUserMapper.selectById(a.getUserId()));
        a.setShDress(shDressMapper.selectById(a.getDrId()));
        }
        if(list.size()>0){
            Result ok = Result.ok().data("userList",list).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改订单状态，归还衣服")
    @PostMapping("/updOrderStatus")
    public void updOrderStatus(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        ShOrder shOrder = shOrderMapper.selectById(id);
        ShDress shDress = shDressMapper.selectById(shOrder.getDrId());
        shDress.setStatus(0);
        shOrder.setStatus("已归还");
        if (shDressMapper.updateById(shDress) > 0&&shOrderMapper.updateById(shOrder)>0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }

    }
    @ApiOperation(value = "根据用户id查询订单")
    @PostMapping("/findAllOrderByUserId")
    public void findAllOrderByUserId(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current,@RequestParam("userId") int userId)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<ShOrder> shUserPage=new Page<>(current,size);
        Page<ShOrder> shUserPages=shOrderMapper.selectPage(shUserPage,wrapper);
        List<ShOrder> list = shUserPages.getRecords();
        int total=shOrderMapper.selectCount(wrapper);
        for(ShOrder a:list){
            a.setShUser(shUserMapper.selectById(a.getUserId()));
            a.setShDress(shDressMapper.selectById(a.getDrId()));
        }
        if(list.size()>0){
            Result ok = Result.ok().data("userList",list).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

